# -*- coding = utf-8 -*-
# @Time : 2020/7/11 11:36
# @Author : gxm

import cv2
import cv2 as cv
import  joblib


def load_model(svc_path,pca_path):
    svc = joblib.load(svc_path)
    pca = joblib.load(pca_path)
    return svc,pca

def nameindex(pre):
    dir=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
         'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U',
         'V', 'W', 'X', 'Y', 'Z', 'chuan', 'zh_e', 'zh_gan', 'zh_gan1', 'zh_gui',
         'zh_gui1', 'zh_hei', 'zh_hu', 'zh_ji', 'zh_jin', 'zh_jing', 'zh_jl',
         '辽', 'zh_lu', 'zh_meng', 'zh_min', 'zh_ning', 'zh_qing', 'zh_qiong',
         'zh_shan', 'zh_su', 'zh_sx', 'zh_wan', 'zh_xiang', 'zh_xin', 'zh_yu', 'zh_yu1',
         'zh_yue', '云', 'zh_zang', 'zh_zhe']
    for i in range(len(dir)):
        if (pre==i):
            return dir[i]


def plate_predict(img_char):
    string=[]
    for step, img in enumerate(img_char):
        img = cv.resize(img, (20, 20), interpolation=cv.INTER_CUBIC)
        test = img.reshape(1, 400)
        # 加载模型
        svc, pca = load_model("D:\pycharmcode\web\shibie\model.m", "D:\pycharmcode\web\shibie\pca.m")
        # svm
        test_x = pca.transform(test)
        pre = svc.predict(test_x)
        # print(pre)
        string.append(nameindex(pre))
    print("车牌号：",string)
    return string


if __name__ == "__main__":
    img = cv2.imread("C:\\Users\\19372\PycharmProjects\\demo_1\\demo2\\pict\\char_3.jpg")
    img_string=plate_predict(img)



